package com.ytktt.study.week9.task3.rpcfx.api.resolver;

import com.ytktt.study.week9.task3.rpcfx.api.RpcfxResolver;
import com.ytktt.study.week9.task3.rpcfx.constant.RpcfxConstant;
import com.ytktt.study.week9.task3.rpcfx.enums.ErrResponseEnum;
import com.ytktt.study.week9.task3.rpcfx.exception.RpcfxException;
import com.ytktt.study.week9.task3.rpcfx.utils.StringFormatUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 反射+泛型解析器
 *
 * @author ytk
 */
public class ReflectGenericResolver implements RpcfxResolver, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object resolve(String beanName) {
        try {
            return this.applicationContext.getBean(beanName);
        } catch (NoSuchBeanDefinitionException e) {
            throw new RpcfxException(ErrResponseEnum.SERVICE_NOT_FOUND_ERROR);
        }
    }
}
